package com.carful.xigua.modules.common.util;

import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author Administrator
 * Date 2024/2/7 16:29
 * Description
 */

public class ControllerClassUtils {
    public static String getClassUri(Class<?> aClass) {
        RequestMapping classAnnotation = aClass.getAnnotation(RequestMapping.class);
        String classPath = "";
        if (classAnnotation != null) {
            classPath = Arrays.stream(classAnnotation.value()).findFirst().orElse("");
        }
        return classPath;
    }

    /**
     * 获取uri + 请求方式
     * @param method
     * @return
     */
    public static String getMethodUri(Method method) {
        RequestMapping annotation = method.getAnnotation(RequestMapping.class);
        if (annotation != null) return Arrays.stream(annotation.value()).findFirst().orElse("") + ":" + Arrays.stream(annotation.method()).map(a->a.name()).findFirst().orElse("");
        GetMapping annotation1 = method.getAnnotation(GetMapping.class);
        if (annotation1 != null) return Arrays.stream(annotation1.value()).findFirst().orElse("") + ":" + RequestMethod.GET.name();
        PostMapping annotation2 = method.getAnnotation(PostMapping.class);
        if (annotation2 != null ) return Arrays.stream(annotation2.value()).findFirst().orElse("") + ":" + RequestMethod.POST.name();
        PutMapping annotation3 = method.getAnnotation(PutMapping.class);
        if (annotation3 != null) return Arrays.stream(annotation3.value()).findFirst().orElse("") + ":" + RequestMethod.PUT.name();
        DeleteMapping annotation4 = method.getAnnotation(DeleteMapping.class);
        if (annotation4 != null) return Arrays.stream(annotation4.value()).findFirst().orElse("") + ":" + RequestMethod.DELETE.name();
        return null;
    }

    /**
     * 获取方法描述
     * @param method
     * @return
     */
    public static String getMethodDesc(Method method) {
//        ApiOperation annotation = method.getAnnotation(ApiOperation.class);
//        if (annotation != null) return annotation.value();
        return getMethodUri(method);
    }
}
